//
//  XZHHomeListDetailViewController.m
//  Piece
//
//  Created by dllo on 16/6/12.
//  Copyright © 2016年 Northeast Dianli University. All rights reserved.
//

#import "XZHHomeListDetailViewController.h"
#import "ZHNetworkHandler.h"
#import "XZHHomeModel.h"
#import "XZHHomeListCollectionViewCell.h"
#import "UIImageView+WebCache.h"
#import "XZHHomeDetailViewController.h"
#import "HYBCardVerticalScaleLayout.h"

@interface XZHHomeListDetailViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *dataSource;
@end

@implementation XZHHomeListDetailViewController

- (void)viewDidLoad {
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0.86 green:0.87 blue:0.88 alpha:1.00];
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:_date style:UIBarButtonItemStylePlain target:nil action:nil];
    [self createTitle:self.date];
    [super viewDidLoad];
    [self setUpCollectionView];
    [self getDataByUrl];
    
}

- (void)setUpCollectionView {
    
//    //创建系统提供的布局对象
//    UICollectionViewFlowLayout *flowLayOut = [[UICollectionViewFlowLayout alloc] init];
//    //设置每个单元格的尺寸
//    flowLayOut.itemSize = CGSizeMake(ScreenWidth / 2 - 7, ScreenHeight / 5);
//    //最小列间距
//    flowLayOut.minimumInteritemSpacing = 0;
//    //最小行间距
//    flowLayOut.minimumLineSpacing = 5;
//    //设置内容区域与周围的距离
//    flowLayOut.sectionInset = UIEdgeInsetsMake(5, 0, 5, 5);
//    //设置滚动方向
//    flowLayOut.scrollDirection = UICollectionViewScrollDirectionVertical;
    HYBCardVerticalScaleLayout *flowLayOut = [[HYBCardVerticalScaleLayout alloc] init];
    
    //初始化collectionView
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(5, 0, ScreenWidth - 5, ScreenHeight - 64 - 49) collectionViewLayout:flowLayOut];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    
    [self.view addSubview:self.collectionView];
    
    //注册cell
    [self.collectionView registerNib:[UINib nibWithNibName:@"XZHHomeListCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"listCell"];
}

- (void)getDataByUrl {
    NSString *url = [NSString stringWithFormat:@"http://v3.wufazhuce.com:8000/api/hp/bymonth/%@", _date];
    [ZHNetworkHandler getDataByURLString:url WithBodyString:nil WithHeaderDic:nil WithResponse:0 WithSuccess:^(id result) {
        self.dataSource = [NSMutableArray array];
        NSDictionary *dic = (NSDictionary *)result;
        for (NSDictionary *dict in [dic objectForKey:@"data"]) {
            XZHHomeModel *model = [[XZHHomeModel alloc] initWithDic:dict];
            [self.dataSource addObject:model];
        }
        [self.collectionView reloadData];
    } WithFailure:^(NSError *error) {
        
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataSource.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    XZHHomeListCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"listCell" forIndexPath:indexPath];
    XZHHomeModel *model = [self.dataSource objectAtIndex:indexPath.row];
    [cell.coverImageView sd_setImageWithURL:[NSURL URLWithString:model.hp_img_url]placeholderImage:[UIImage imageNamed:@"home_cover_placeholder"]];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    XZHHomeDetailViewController *detailVC = [[XZHHomeDetailViewController alloc] init];
    detailVC.model = [self.dataSource objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:detailVC animated:YES];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
